package com.lx.constant.segment.common;

import com.lx.constant.consts.CommonConstant;
import com.lx.constant.segment.Segment;
import com.lx.wrapper.AbstractWrapper;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.query.Query;

/**
 * 分页语句
 *
 * @author lx
 * @create 2022/11/14 22:44
 **/
@Getter
@Setter
public class LimitSegment extends Segment {

    /**
     * 起始标志量
     */
    private Integer currentSize;

    /**
     * 分页大小
     */
    private Integer pageSize;

    @Override
    public String convertSegmentToQL(AbstractWrapper parentWrapper, AbstractWrapper thisWrapper, boolean isHQL) {
        return isHQL ? null : currentSize + CommonConstant.COMMA + pageSize;
    }

    @Override
    public void setHibernateParamByQuery(boolean isHQL, Query query) {
        if (isHQL) {
            query.setFirstResult(getCurrentSize());
            query.setMaxResults(getPageSize());
        }
    }

    @Override
    public boolean needReplace(Segment t1) {
        return t1 != null;
    }
}
